package com.zllkj.mm.module.freetime

import androidx.recyclerview.widget.LinearLayoutManager
import com.zllkj.mm.R
import com.zllkj.mm.adapter.FreeTimeConfirmHistoryListAdapter
import com.zllkj.mm.adapter.FreeTimeWaitToConfirmListAdapter
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseFragment
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.model.entity.*
import com.zllkj.mm.module.home.fragments.MyCollectionFragment
import com.zllkj.mm.module.shopping.fragment.OrderListItemFragment
import kotlinx.android.synthetic.main.fragment_mycollection.view.*
import java.util.HashMap

class OrderVerificationHistoryFragment: BaseFragment() {

    var datas = ArrayList<WriteOffFreeTimeHistoryListVoRecord>()
    lateinit var adapter : FreeTimeConfirmHistoryListAdapter
    var currentPage = 1
    override fun getLayoutId(): Int {
       return R.layout.fragment_mycollection
    }


    override fun loadData() {
        super.loadData()


        adapter = FreeTimeConfirmHistoryListAdapter(R.layout.item_freetime_verification_history,datas)
        mRootView.rv_mycollection.layoutManager = LinearLayoutManager(requireContext())
        mRootView.rv_mycollection.adapter = adapter


        adapter.setOnItemClickListener { baseQuickAdapter, view, i ->

        }


        mRootView?.smartRefreshlayout_mycollection?.setOnRefreshListener {
            datas.clear()
            currentPage = 1
            loadNetWorkData(OrderListItemFragment.PULL_DOWN)
        }

        mRootView?.smartRefreshlayout_mycollection?.setOnLoadMoreListener {
            currentPage += 1
            loadNetWorkData(OrderListItemFragment.LOAD_MORE)
        }
        loadNetWorkData(-1)
    }


    fun loadNetWorkData(loadStatus:Int){
        addSubscribe(RetrofitHelper.getInstance().freeTimeApi.getWriteOffListHistory((HashMap<String, Any>().apply {
            this["goodsName"] = ""
            this["currentPage"] = currentPage
            this["pageSize"] = 20
        })).compose<WriteOffFreeTimeHistoryListVo>(RxHelper.defaultHandleResult<WriteOffFreeTimeHistoryListVo>()).subscribe(object : ProgressSubscriber<WriteOffFreeTimeHistoryListVo>(requireContext()){
            override fun onSucc(t: WriteOffFreeTimeHistoryListVo?) {
                t?.records?.map {
                    datas.add(it)
                    adapter.notifyDataSetChanged()
                }


                if(loadStatus == OrderListItemFragment.PULL_DOWN) {
                    mRootView.smartRefreshlayout_mycollection.finishRefresh()
                    adapter.notifyDataSetChanged()
                } else if (loadStatus == OrderListItemFragment.LOAD_MORE) {
                    mRootView.smartRefreshlayout_mycollection.finishLoadMore()
                    adapter.notifyDataSetChanged()
                } else {
                    adapter.notifyDataSetChanged()
                }
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

    }


}